package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.Fund;
import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.AgreeRefundReq;
import com.ymatou.iapi.optrade.parameter.req.ApplyRefundReq;
import com.ymatou.iapi.optrade.parameter.vo.SalesRefundInfoVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.AgreeRefundCall;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.synctrade.parameter.mongo.Order;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.userquery.service.GetOrderFundInfoCall;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFundInfoBean;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFundInfoResponse;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.common.order.userquery.util.MongoHelper;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.mongo.OrderQueryWapper;
import com.ymttest.database.mongo.config.OrderField;
import com.ymttest.utils.DataUtil;

/**
 * 获取订单资金信息
 */
@TestSuite
public class Ts_GetOrderFundInfo {
	private static GetOrderFundInfoBean getOrderFundInfoBean;
	private static GetOrderFundInfoCall getOrderFundInfoCall;
	
	private static Integer userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单资金信息");
		
		String accountid1;
		try {
			accountid1 = AccountCallServiceV2.getAccountId(20238699);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					9000000), new BigDecimal(0));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		getOrderFundInfoBean = new GetOrderFundInfoBean();

		getOrderFundInfoCall = new GetOrderFundInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getOrderFundInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		
		GetOrderFundInfoResponse expect = new GetOrderFundInfoResponse();
		ConstructExpectedResponse(expect);
		GetOrderFundInfoResponse actual = getOrderFundInfoCall
				.getOrderFundInfoResponse();
		DataUtil.verifyEquals(expect.getBuyerLoginId(),
				actual.getBuyerLoginId(), "验证BuyerLoginId");
		DataUtil.verifyDecimalEquals(expect.getBuyerPaidCashAmount(),
				actual.getBuyerPaidCashAmount(), "验证BuyerPaidCashAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerRefundedCashAmount(),
				actual.getBuyerRefundedCashAmount(),
				"验证BuyerRefundedCashAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerRefundedGiftAmount(),
				actual.getBuyerRefundedGiftAmount(),
				"验证BuyerRefundedGiftAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerUsedCouponAmount(),
				actual.getBuyerUsedCouponAmount(), "验证BuyerUsedCouponAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerUsedGiftAmount(),
				actual.getBuyerUsedGiftAmount(), "验证BuyerUsedGiftAmount");
		DataUtil.verifyEquals(expect.getOrderId(), actual.getOrderId(),
				"验证OrderId");
		DataUtil.verifyEquals(expect.getOrderStatus(), actual.getOrderStatus(),
				"验证OrderStatus");
		 DataUtil.verifyDecimalEquals(expect.getRefundRelatedCouponAmount(),
		 actual.getRefundRelatedCouponAmount(),
		 "验证RefundRelatedCouponAmount");
		DataUtil.verifyDecimalEquals(expect.getRefundRelatedCouponAmount(),
				DataUtil.BigDecimall.GetValueOrDefault(actual
						.getRefundRelatedCouponAmount()),
				"验证RefundRelatedCouponAmount");
		DataUtil.verifyEquals(expect.getSellerLoginId(),
				actual.getSellerLoginId(), "验证SellerLoginId");
		DataUtil.verifyEquals(expect.getBuyerId(), actual.getBuyerId(),
				"验证BuyerId");
		DataUtil.verifyEquals(expect.getSellerId(), actual.getSellerId(),
				"验证SellerId");
		 DataUtil.verifyEquals(expect.getBuyerUsedCouponCode(),
		 actual.getBuyerUsedCouponCode(), "验证BuyerUsedCouponCode");

		DataUtil.verifyEquals(null, actual.getBuyerUsedCouponCode(),
				"验证BuyerUsedCouponCode");
	}

	private void ConstructExpectedResponse(GetOrderFundInfoResponse response) {

		Query query = Query.query(Criteria.where(OrderField.ID).is(
				getOrderFundInfoBean.getOrderId()));
		MongoHelper.generateField(query.fields(), OrderField.ID,
				OrderField.TRADING_STATUS, OrderField.USER_ID,
				OrderField.USER_LOGIN, OrderField.SELLER_ID,
				OrderField.SELLER_LOGIN, OrderField.FUND);
		Order order = new OrderQueryWapper().findOneByQuery(query);

		if (null != order) {

			response.setBuyerId(order.getUserId().intValue());
			response.setBuyerLoginId(order.getUserLogin());
			response.setSellerId(order.getSellerId().intValue());
			response.setSellerLoginId(order.getSellerLogin());
			response.setOrderId(order.getId());
			response.setOrderStatus(order.getTradingStatus());
			
			Fund fund = order.getFund();

			if (fund != null) {

				response.setBuyerPaidCashAmount(fund.getPaidCash().add(
						fund.getPostCash()));
				response.setBuyerRefundedCashAmount(fund.getRefundCash());
				response.setBuyerRefundedGiftAmount(fund.getRefundGif());
				response.setBuyerUsedCouponAmount(fund.getYmtCoupon());
				response.setBuyerUsedCouponCode(order.getYmtCouponCode());
				response.setRefundRelatedCouponAmount(fund.getRefundCoupon());
				response.setBuyerUsedGiftAmount(Utils.zeroIfNull(fund.getPaidGif())
						.add(Utils.zeroIfNull(fund.getPostGif())));
			}
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_001() {
		Logger.start(true, "获取订单资金信息成功,订单状态为1");
		try {

			Logger.comment("准备前置数据");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();

			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_002() {
		Logger.start(true, "获取订单资金信息成功，订单状态为2");
		try {
			Logger.comment("准备前置数据");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long userId = placeOrder.getUserId();

			long tradingId = placeOrder.getTradingId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			// 支付订单
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					tradingId);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();

			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_003() {
		Logger.start(true, "获取订单资金信息成功，订单状态为3");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			long userId = placeOrder.getUserId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			// 取消订单
			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_004() {
		Logger.start(true, "获取订单资金信息成功，订单状态为4, 收货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			TradingCallService.confirmReceive(userId, orderId);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_005() {
		Logger.start(true, "获取订单资金信息成功，订单状态为4, 申请退款");
		try {
			Logger.comment("准备前置数据");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			com.ymatou.iapi.optrade.resp.Order order = placeOrderResp.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			Long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			// 支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);
			
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);
			
			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			RefundService.applyRefund(order);

			Thread.sleep(5000);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_006() {
		Logger.start(true, "获取订单资金信息成功，订单状态为4, 退款成功");
		try {
			Logger.comment("准备前置数据");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			com.ymatou.iapi.optrade.resp.Order order = placeOrderResp.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			Long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			// 支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);
			
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);
			
			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			ApplyRefundReq applyRefundReq = RefundService.applyRefund(order);

			Thread.sleep(5000);
			
			AgreeRefundReq salesrefundsuccessReq = new AgreeRefundReq();
			AgreeRefundCall salesrefundsuccessCall = new AgreeRefundCall();
			
			salesrefundsuccessReq.setOrderId(orderId);
			
			int i = 0;
			for (SalesRefundInfoVo salesRefundInfoVo : applyRefundReq
					.getSalesRefundInfoList()) {

				salesrefundsuccessReq.setRefundBillNo(salesRefundInfoVo
						.getRefundBillNo());
				salesrefundsuccessCall.setData(salesrefundsuccessReq);

				Logger.comment(String.format("orderid: %s 第 %s 个商品 退款执行",
						orderId, i + 1));

				salesrefundsuccessCall.callService();

				Logger.verifyEquals("true",
						salesrefundsuccessCall.getString("success"),
						"验证退货退款成功返回");
				i++;
			}
			
			Thread.sleep(5000);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_008() {
		Logger.start(true, "获取订单资金信息成功，订单状态为12");
		try {
			Logger.comment("准备前置数据");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long userId = placeOrder.getUserId();
			
			// 取消订单
			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
			TradingSyncService.getSyncResult(orderId);
			Logger.comment("准备前置数据结束");

			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_009() {
		Logger.start(true, "获取订单资金信息成功，订单状态为13");
		try {
			Logger.comment("准备前置数据");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long userId = placeOrder.getUserId();
			
			// 取消订单
			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.SELLER_CANCEL_ORDER, userId);
			TradingSyncService.getSyncResult(orderId);
			Logger.comment("准备前置数据结束");
			
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_010() {
		Logger.start(true, "获取订单资金信息成功，订单状态为18");
		try {
			Logger.comment("准备前置数据");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long userId = placeOrder.getUserId();
			
			// 取消订单
			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.SYSTEM_CANCEL_ORDER, userId);
			TradingSyncService.getSyncResult(orderId);
			Logger.comment("准备前置数据结束");
			
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
			
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_012() {
		Logger.start(true, "获取订单资金信息成功，订单状态为17,bpaidinfull=1");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			getOrderFundInfoBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();

			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}